package com.greate.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.greate.community.dao.UserInfoMapper;
import com.greate.community.entity.UserInfo;
import com.greate.community.service.UserInfoService;
import com.greate.community.util.BusCodeEnum;
import com.greate.community.util.BusinessException;
import com.greate.community.util.CommunityUtil;
import com.greate.community.util.HostHolder;
import org.apache.catalina.Host;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private HostHolder hostHolder;

    @Override
    public void addUserInfo(UserInfo info) {
        userInfoMapper.insert(info);
    }

    @Override
    public UserInfo getInfoByUserId(int userId) {
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserInfo::getUserId, userId);
        return userInfoMapper.selectOne(wrapper);
    }

    @Override
    public void updateInfo(Long id, String gender, String phone, String department, Integer grade, String specialities, String signature) {
        UserInfo info = new UserInfo();
        info.setId(id);
        info.setUserId(hostHolder.getUser().getId());
        info.setSex(CommunityUtil.getGenderIndex(gender));
        info.setPhone(phone);
        info.setDepartment(department);
        info.setGrade(grade);
        info.setSpecialities(specialities);
        info.setSignature(signature);
        try {
            userInfoMapper.updateById(info);
        }catch (Exception e){
            throw new BusinessException(BusCodeEnum.FAILED);
        }
    }

}
